package com.carey.myblog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.carey.myblog.pojo.Comment;
import com.carey.myblog.pojo.Message;
import com.carey.myblog.service.MessageService;
import com.carey.myblog.mapper.MessageMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
* @author carey-
* @description 针对表【t_message】的数据库操作Service实现
* @createDate 2022-05-29 22:44:07
*/
@Service
public class MessageServiceImpl extends ServiceImpl<MessageMapper, Message>
    implements MessageService{

    @Autowired
    private JavaMailSender javaMailSender;

    @Value("${email}")
    private String authoremail;

    @Override
    public Page<Message> getMessage(Integer current, Integer limmit) {
        //先查父评论
        QueryWrapper<Message> wrapper = new QueryWrapper<>();
        wrapper.eq("parentid",0);
        wrapper.orderByDesc("create_time");
        List<Message> messageList = baseMapper.selectList(wrapper);
        //List<Message> messageList2 = messageList;
        for (Message message : messageList) {
            List<Message> twomes= new ArrayList<>(); //用于存放：各个2级评论的（2级评论，包含所有子代）
            this.recursion(twomes, message);            //递归：查询当前sonComment子代，全放入2级评论
            message.setReplyMessages(twomes);
        }
        //将list集合转换成page对象
        Page<Message> page = new Page<>(current,limmit);
        // 当前页第一条数据在List中的位置
        int start = (current-1)*limmit;
        // 当前页最后一条数据在List中的位置
        int end = (int)((start + page.getSize()) > messageList.size() ? messageList.size() : (page.getSize() * page.getCurrent()));
        page.setRecords(messageList.subList(start,end));
        page.setTotal(messageList.size());
        return page;
    }

    @Override
    public void saveMessage(Message message) {
        //不能直接getparentid
        Long parentid = message.getParentMessage().getId();
        if (parentid!=null){
            //回复
            Message parentMessage = baseMapper.selectById(parentid);
            message.setParentid(parentid);
            message.setParentMessage(parentMessage);
        }else {
            //不是回复
            message.setParentMessage(null);
        }
        baseMapper.insert(message);
    }

    @Override
    public void sendemail(Message message) {
        //判断是否为父评论
        Message parentMessage = message.getParentMessage();
        if (parentMessage==null){
            return;
        }else {
            //判断父评论是否开启短信通知
            Long id = parentMessage.getId();
            Message pmessage = baseMapper.selectById(id);
            if (parentMessage.getReplyinform()){
                SimpleMailMessage smm = new SimpleMailMessage();
                smm.setFrom(authoremail);
                smm.setTo(pmessage.getEmail());
                //smm.setCc("2244413146@qq.com");//抄送人
                smm.setSubject("Carey_blog:收到一条来自"+message.getNickname()+"的回复");//邮件主题
                smm.setText(message.getContent());//邮件内容
                javaMailSender.send(smm);
            }
        }

    }

    //递归：查询当前sonComment子代，全放入2级评论
    private void recursion(List<Message> twoMes, Message sonMes){
        if(sonMes != null){     //首先判断：子代是否为null
            //不为null则：查询到它的下一代
            QueryWrapper<Message> wrapper = new QueryWrapper<>();
            wrapper.eq("parentid",sonMes.getId());
            List<Message> messages = baseMapper.selectList(wrapper);
            //判断该节点是否有回复
            if (messages.size()>0){
                for(Message message:messages){
                    //需要重新设置ParentComment
                    message.setParentMessage(baseMapper.selectById(message.getParentid()));
                    //将当前：子节点，放入2级评论
                    twoMes.add(message);
                    //对该子节点：进一步递归判断
                    recursion(twoMes, message);
                }
            }

        }
    }
}




